﻿/**
 *    Copyright 2012 Yohan Courbe
 *
 *  Licensed under the Apache License, Version 2.0 (the "License") 
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *      
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SharpGL;

namespace NetworkAnalyzer.Engines.Graphics.HttpHits
{
    public partial class GraphicsHttpHitsForm : Form
    {

        private static PointF _screenCenterPoint = new PointF(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);

        public GraphicsHttpHitsForm()
        {
            InitializeComponent();
            GraphicsHttpHits.StartSystem();
        }

        private void openGLControl_OpenGLDraw(object sender, PaintEventArgs e)
        {
            OpenGL gl = openGLControl.OpenGL;
            //  Load the identity matrix.
            gl.LoadIdentity();
            //  Clear the color and depth buffer.
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            lock (GraphicsHttpHits.Locker)
            {
                foreach (var httpHit in GraphicsHttpHits.HttpHits.Where<HttpHit>(httpHit => httpHit.Activity > 5))
                {
                    //Console.WriteLine("{0} {1}", httpHit.Domain, httpHit.CalculateFontSize
                    gl.DrawText((int) httpHit.Point.X, (int) httpHit.Point.Y, (float)httpHit.Color.R / 255f, (float)httpHit.Color.G / 255f, (float)httpHit.Color.B / 255f, httpHit.Font, httpHit.CalculateFontSize(), httpHit.Domain);
                }
            }


            //gl.Begin(SharpGL.Enumerations.BeginMode.Points);
            //gl.Color(1.0f, 1.0f, 1.0f);
            //gl.Vertex(_screenCenterPoint.X, _screenCenterPoint.Y);

            //for (int i = 0; i < 180; i++)
            //{
            //    var point = Tools.PointFromPolarCoordinate(100, i * 2);
            //    gl.Vertex(_screenCenterPoint.X + point.X, _screenCenterPoint.Y + point.Y);
            //}

            //gl.End();

        }

        private void openGLControl_OpenGLInitialized(object sender, EventArgs e)
        {
            OpenGL gl = openGLControl.OpenGL;

            //  Set the clear color.
            gl.ClearColor(0, 0, 0, 0);
            gl.ShadeModel(OpenGL.GL_SMOOTH);
            gl.Enable(OpenGL.GL_BLEND);							// Enable Blending
            gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE);
        }

        private void openGLControl_Resized(object sender, EventArgs e)
        {
            //  TODO: Set the projection matrix here.

            //  Get the OpenGL object.
            OpenGL gl = openGLControl.OpenGL;

            //  Set the projection matrix.
            gl.MatrixMode(OpenGL.GL_PROJECTION);

            //  Load the identity.
            gl.LoadIdentity();

            gl.Ortho(0, 1680, 1024, 0, 0, 1);

            //  Set the modelview matrix.
            gl.MatrixMode(OpenGL.GL_MODELVIEW);
        }

        private void GraphicsHttpHitsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GraphicsHttpHits.StopSystem();
        }
    }
}